package com.rtx.edi.platform.application.controller;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rtx.edi.model.apibaseinfo.po.PlApibaseinfo;
import com.rtx.edi.model.application.dto.PlApplicationDto;
import com.rtx.edi.model.application.po.PlApplication;
import com.rtx.edi.model.application.po.PlApplicationLink;
import com.rtx.edi.model.response.EdiResponse;
import com.rtx.edi.platform.application.service.PlApplicationLinkService;
import com.rtx.edi.platform.application.service.PlApplicationService;
import com.rtx.edi.platform.baseinfo.service.PlApibaseinfoService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 应用信息表  前端控制器
 * </p>
 *
 * @author abc
 * @since 2023-03-06
 */
@Api(tags = {"应用管理"})
@RestController
@RequestMapping("/v1/plApplication")
@CrossOrigin
public class PlApplicationController {

    @Autowired
    private PlApplicationService plApplicationService;
    
    @Autowired
    private PlApplicationLinkService plApplicationLinkService;
    
    @Autowired
    PlApibaseinfoService plApibaseinfoService;

    /**
     * 获取应用列表
     * @return
     */
    @ApiOperation(value = "应用列表", notes = "分页获取应用列表")
    @PostMapping("selectPlApplicationList")
    public EdiResponse selectPlApplicationList(@RequestBody PlApplicationDto plApplicationDto){
        PlApplication plApplication = new PlApplication();
        BeanUtils.copyProperties(plApplicationDto,plApplication);
        Page<PlApplication> pageParam = new Page<>(plApplicationDto.getPageNum(), plApplicationDto.getPageSize());
        plApplicationService.selectPlApplicationList(pageParam,plApplication);
        return EdiResponse.success().data("page",pageParam);
    }

    /**
     * 根据ID获取应用
     * @param plApplicationDto
     * @return
     */
    @ApiOperation(value = "查询应用", notes = "根据ID查询应用")
    @PostMapping("selectPlApplicationById")
    public EdiResponse selectPlApplicationById(@RequestBody PlApplicationDto plApplicationDto){
        return EdiResponse.success().data("item",plApplicationService.getById(plApplicationDto.getId()));
    }
    /**
     * 根据id删除
     * @param plApplicationDto
     * @return
     */
    @ApiOperation(value = "删除应用", notes = "根据id删除应用", httpMethod = "POST")
    @PostMapping("deletePlApplicationById")
    public EdiResponse deletePlApplicationById(@RequestBody PlApplicationDto plApplicationDto) {
    	QueryWrapper<PlApplicationLink> linkQW = new QueryWrapper<PlApplicationLink>();
    	linkQW.eq("above_application_id", plApplicationDto.getId());
    	boolean exists = plApplicationLinkService.getBaseMapper().exists(linkQW);
    	if(exists) {
    		EdiResponse.failed().setReturnMgr("应用存在应用关联不能删除！");
    	}
    	
       	QueryWrapper<PlApplicationLink> linkQW2 = new QueryWrapper<PlApplicationLink>();
       	linkQW2.eq("below_application_id", plApplicationDto.getId());
    	boolean exist2 = plApplicationLinkService.getBaseMapper().exists(linkQW2);
    	if(exist2) {
    		EdiResponse.failed().setReturnMgr("应用存在应用关联不能删除！");
    	}
    	
    	QueryWrapper<PlApibaseinfo> apiQW = new QueryWrapper<PlApibaseinfo>();
    	apiQW.eq("application_id", plApplicationDto.getId());
    	boolean apiWxists = plApibaseinfoService.getBaseMapper().exists(apiQW);
    	if(apiWxists) {
    		EdiResponse.failed().setReturnMgr("应用存在API不能删除！");
    	}
    	
        return plApplicationService.removeById(plApplicationDto.getId()) ? EdiResponse.success() : EdiResponse.failed();
    }
    
    /**
     * 根据id列表删除
     * @param list
     * @return
     */
    @ApiOperation(value = "批量删除应用", notes = "根据id列表删除应用", httpMethod = "POST")
    @PostMapping("deletePlApplicationByIds")
    public EdiResponse deletePlApplicationByIds(@RequestBody List<PlApplicationDto> list) {

    	for(PlApplicationDto plApplicationDto:list) {
    		QueryWrapper<PlApplicationLink> linkQW = new QueryWrapper<PlApplicationLink>();
    		linkQW.eq("above_application_id", plApplicationDto.getId());
    		boolean exists = plApplicationLinkService.getBaseMapper().exists(linkQW);
    		if(exists) {
    			return EdiResponse.failed().returnMgr("应用存在应用关联不能删除！");
    		}

    		QueryWrapper<PlApplicationLink> linkQW2 = new QueryWrapper<PlApplicationLink>();
    		linkQW2.eq("below_application_id", plApplicationDto.getId());
    		boolean exist2 = plApplicationLinkService.getBaseMapper().exists(linkQW2);
    		if(exist2) {
    			return EdiResponse.failed().returnMgr("应用存在应用关联不能删除！");
    		}

    		QueryWrapper<PlApibaseinfo> apiQW = new QueryWrapper<PlApibaseinfo>();
    		apiQW.eq("application_id", plApplicationDto.getId());
    		boolean apiWxists = plApibaseinfoService.getBaseMapper().exists(apiQW);
    		if(apiWxists) {
    			return EdiResponse.failed().returnMgr("应用存在API不能删除！");
    		}
    	}
    	
    	try {
    		plApplicationService.deletePlApplicationByIds(list);
    		return EdiResponse.success();
    	} catch (Exception e) {
    		return EdiResponse.failed().returnMgr(e.getMessage());
    	}

    }

    /**
     * 新增应用
     * @param plApplicationDto
     * @return
     */
    @ApiOperation(value = "新增应用")
    @PostMapping("addPlApplication")
    public EdiResponse addPlApplication(@RequestBody PlApplicationDto plApplicationDto){
//		Subject subject = SecurityUtils.getSubject();
//		Session session = subject.getSession();
//		String companyid = (String) Optional.ofNullable(session.getAttribute("companyid")).orElse("1");
//		plApplicationDto.setCompanyId(Long.parseLong(companyid));
		QueryWrapper<PlApplication> qw = new QueryWrapper<PlApplication>();
		qw.eq("application_name", plApplicationDto.getApplicationName());
		qw.eq("company_id", plApplicationDto.getCompanyId());
		boolean exists = plApplicationService.getBaseMapper().exists(qw);
		if(exists) {
			return EdiResponse.failed().returnMgr("应用名称已存在不允许重复创建");
		}
		
        PlApplication plApplication = new PlApplication();
        BeanUtils.copyProperties(plApplicationDto,plApplication);
        return plApplicationService.addPlApplication(plApplication) ? EdiResponse.success() : EdiResponse.failed();
    }

    /**
     * 根据ID修改应用
     * @return
     */
    @ApiOperation(value = "修改应用", notes = "根据ID修改应用")
    @PostMapping("editPlApplicationById")
    public EdiResponse editPlApplicationById(@RequestBody PlApplicationDto plApplicationDto){
        PlApplication plApplication = new PlApplication();
        BeanUtils.copyProperties(plApplicationDto,plApplication);
        return plApplicationService.updateById(plApplication) ? EdiResponse.success() : EdiResponse.failed();
    }
}
